<script>
	// const baseUrl = 'http://wxamp100.natapp1.cc'
	// // const baseUrl = 'https://new.mkxd.cn/wxapp'

	import {
		baseUrl
	} from '@/common/config.js'

	export default {
		onLaunch: function(options) {
			uni.setStorageSync('city', '石家庄')
			uni.setStorageSync('cityId', 130100)
			uni.hideTabBar()
			// #ifdef H5
			uni.setStorageSync('token',
			'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJsb2dpblR5cGUiOiJsb2dpbiIsImxvZ2luSWQiOiJ4Y3hfdXNlcjoxODI5ODY4NDUwODIwMjM1MjY2Iiwicm5TdHIiOiJSN1Z3eWoycW55M2lBMURjblo1dXBDcmdQSkZMVjNlMSIsImNsaWVudGlkIjoiYmU3MDUyYTdlNGY4MDJjMjBkZjEwYThkMTMxYWRiMTIiLCJ1c2VySWQiOjE4Mjk4Njg0NTA4MjAyMzUyNjYsInVzZXJOYW1lIjoi5pGp5Y2h5YWE5byfZmFrV0QifQ.0NGk0c64wxfDrlRBxq2owosAJA68mgXVQqSQ6eIhaxw')
			this.$store.dispatch('actionsHandleIsLoginChange', false);
			// #endif

			if (uni.getStorageSync('token')) {
				uni.request({
					url: baseUrl + '/xcx/user/getInfo',
					header: {
						Authorization: 'Bearer' + ' ' + uni.getStorageSync('token'),
						'content-type': 'application/json;charset=UTF-8',
						'clientId': 'be7052a7e4f802c20df10a8d131adb12'
					},
					success: (res) => {
						if (res.data.code == 200) {
							uni.setStorageSync('userInfo', res.data.data)
							this.$store.dispatch('actionsHandleUserInfoChange', res
								.data.data);
							this.$store.dispatch('actionsHandleIsLoginChange',
								true);
							this.$store.dispatch('actionsHandleTokenChange', uni.getStorageSync('token'));
							this.$isResolve()
						} else if (res.data.code == 401) {
							uni.clearStorageSync()
							this.$store.dispatch('actionsHandleUserInfoChange', '');
							this.$store.dispatch('actionsHandleIsLoginChange', false);
							this.$store.dispatch('actionsHandleTokenChange', '');
							uni.navigateTo({
								url: '/pages/log/log'
							})
							this.$isResolve()
						} else if (res.data.code == 302) {
							this.$store.dispatch('actionsHandleIsLoginChange',
								true)
							this.$store.dispatch('actionsHandleUserInfoChange', uni.getStorageSync(
								'userInfo'));
							this.$isResolve()
						} else {
							this.$isResolve()
						}
					}
				})
			} else {
				this.$store.dispatch('actionsHandleIsLoginChange', false);
				this.$nextTick(() => {
					this.$isResolve()
				})
			}
		},
		onShow: function(options) {
			uni.hideTabBar()
			// #ifdef MP
			this.autoUpdate()
			// #endif
			console.log('App Show')
			// console.log('扫码参数=======', options.query)
			// if (options.query.doorId) {
			// 	console.log('扫码参数=======', options.query.doorId)
			// 	uni.navigateTo({
			// 		url: '/packageA/scanOpenDoor/scanOpenDoor?doorId=' + options.query.doorId
			// 	})
			// }
		},
		onHide: function() {
			console.log('App Hide')
		},
		onLoad: function(options) {},
		methods: {
			autoUpdate() {
				const self = this
				// 获取小程序更新机制兼容
				if (uni.canIUse('getUpdateManager')) {
					const info = uni.getAccountInfoSync()
					const updateManager = uni.getUpdateManager()
					// 1. 检查小程序是否有新版本发布
					updateManager.onCheckForUpdate((res) => {
						// 请求完新版本信息的回调
						if (res.hasUpdate) {
							// 检测到新版本，需要更新，给出提示
							wx.showModal({
								title: '更新提示',
								content: '检测到新版本，是否下载新版本并重启小程序？',
								showCancel: false, // 隐藏取消按钮
								confirmText: '确定更新', // 只保留确定更新按钮
								success({
									confirm
								}) {
									if (confirm) {
										// 2. 用户确定下载更新小程序，小程序下载及更新静默进行
										self.downLoadAndUpdate(updateManager)
									}
								}
							})
						}
					})
				} else {
					// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
					// uni.showModal({
					// 	title: '提示',
					// 	content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
					// })
				}
			},
			/**
			 * 下载小程序新版本并重启应用
			 */
			downLoadAndUpdate: (updateManager) => {
				uni.showLoading()
				// 静默下载更新小程序新版本
				updateManager.onUpdateReady(() => {
					uni.hideLoading()
					// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
					updateManager.applyUpdate()
				})
				updateManager.onUpdateFailed(() => {
					// 新的版本下载失败
					uni.showModal({
						title: '已经有新版本了哟~',
						content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
					})
				})
			},
			getUserInfo() {
				this.$req.get('/xcx/user/getInfo').then(res => {
					uni.hideLoading()
					uni.setStorageSync('userInfo', res.data.data)
					this.$store.dispatch('actionsHandleUserInfoChange', res
						.data.data);
					this.$store.dispatch('actionsHandleIsLoginChange',
						true);
					uni.navigateBack()
				})
			}
		}
	}
</script>
<style>
	page{
		background: #F4F5FA;
	}
</style>
<style lang="scss">
	@import url('@/static/css/common.css');
	@import "@/uni_modules/uview-ui/index.scss";
	/*每个页面公共css */

	.rowbox {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}

	.columnbox {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
	}

	.wdh-100 {
		width: 100%;
	}

	.spb {
		justify-content: space-between;
	}

	.jus {
		justify-content: flex-start;
	}

	.als {
		align-items: flex-start;
	}

	.def-col {
		color: #126BC9;
	}

	.def-bgcol {
		background: #126BC9;
	}

	.textovflow {
		text-overflow: ellipsis;
		white-space: nowrap;
		overflow: hidden;
	}

	.grey-btn {
		background: #c9ceca;
	}

	.small-btn {
		font-size: 28rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		box-sizing: border-box;
		background: #17698F;
		color: #fff;
		padding: 10rpx 20rpx;
		border-radius: 100rpx;
		line-height: 40rpx;
	}

	.bottom-box {
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 9;
		background: #fff;
		padding: 20rpx 40rpx;
		padding-bottom: 40rpx;
		box-shadow: inset 0px 2rpx 5rpx 0px rgba(169, 179, 190, 0.25);

	}

	.send-btn {
		background: #17698F;
		height: 82rpx;
		font-size: 28rpx;
		color: #FFFFFF;
		font-weight: bold;
		border-radius: 20rpx;
	}
</style>